package com.jy.importclient.handler;

import com.jy.importclient.common.constants.ReqConstants;
import com.jy.importclient.common.dto.ResDto;
import com.jy.importclient.common.dto.StartTaskDto;
import com.jy.importclient.common.util.FileUtil;
import com.jy.importclient.common.util.StringUtil;
import com.jy.importclient.properties.HttpProperty;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @类名 HttpRequestHandler
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/4/25 21:27
 * @版本 1.0
 */
@Component
public class ReqSqlldrHandler extends AbsReqSqlldrHandler {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private HttpProperty httpProperty;

    @Override
    public ResDto createTask(String sqlldrUrl) {
        String url = StringUtil.concat(new StringBuffer(), ReqConstants.PROTOCOL, sqlldrUrl, httpProperty.getUris()[0]);
        return restTemplate.getForObject(url, ResDto.class);
    }

    @Override
    public ResDto upload(String sqlldrUrl, String filePath, String api) {
        //设置请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/octet-stream");

        //获取文件流
        byte[] buffer = FileUtil.getBytes(filePath);

        //绑定请求体参数和请求头
        HttpEntity<byte[]> httpEntity = new HttpEntity<>(buffer, httpHeaders);

        String url = ReqConstants.PROTOCOL + sqlldrUrl + api;

        //发起put请求
        ResponseEntity<ResDto> response = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, ResDto.class);
        return response.getBody();
    }

    @Override
    public ResDto startTask(String sqlldrUrl, StartTaskDto startTaskDto) {
        String url = StringUtil.concat(new StringBuffer(), ReqConstants.PROTOCOL, sqlldrUrl, httpProperty.getUris()[1]);
        return restTemplate.postForObject(url, startTaskDto, ResDto.class);
    }

    @Override
    public ResDto checkResult(String sqlldrUrl, String taskId) {
        String url = StringUtil.concat(new StringBuffer(), ReqConstants.PROTOCOL, sqlldrUrl, httpProperty.getUris()[2], "?taskId={taskId}");
        ResponseEntity<ResDto> response = restTemplate.getForEntity(url, ResDto.class, taskId);
        return response.getBody();
    }

    @Override
    public String downloads(String downloadFilePath, String sqlldrUrl, String file) throws IOException {
        if (!StringUtils.isEmpty(file)) {
            String url = StringUtil.concat(new StringBuffer(), ReqConstants.PROTOCOL, sqlldrUrl, file);
            ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, byte[].class);
            byte[] buffer = response.getBody();
            if (buffer != null) {
                //获取文件名称
                String fileName = FileUtil.getFileName(file);
                //生成文件路径
                String filePath = FileUtil.createDirectories(downloadFilePath + "/file_log_discard_bad");
                filePath = StringUtil.concat(new StringBuffer(), filePath, "/", fileName);
                try (BufferedOutputStream ous = new BufferedOutputStream(new FileOutputStream(filePath))) {
                    ous.write(buffer);
                    ous.flush();
                    return filePath;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
